package com.gomore.opple.utils;


import android.text.TextUtils;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Company 上海勾芒科技有限公司.
 * Created by asus on 16/12/25.
 */
@SuppressWarnings("unchecked")
public class JacksonUtils {


    public JacksonUtils() {
    }

    public static <T> String toJson(T bean) {
        if (bean == null) {
            return null;
        } else {
            StringWriter sw = new StringWriter();

            try {
                JsonGenerator e = (new JsonFactory()).createJsonGenerator(sw);
                JacksonJsonMapper.getInstance().writeValue(e, bean);
                e.close();
                return sw.toString();
            } catch (JsonGenerationException var3) {
                var3.printStackTrace();
            } catch (JsonMappingException var4) {
                var4.printStackTrace();
            } catch (IOException var5) {
                var5.printStackTrace();
            }

            return null;
        }
    }

    public static <T> T fromJson(String json, Class<T> clzz) throws Exception {
        if (TextUtils.isEmpty(json)) {
            return null;
        } else {
            Object t = null;

            try {
                t = JacksonJsonMapper.getInstance().readValue(json, clzz);
                return (T) t;
            } catch (JsonParseException var4) {
                throw var4;
            } catch (JsonMappingException var5) {
                throw var5;
            } catch (IOException var6) {
                throw var6;
            }
        }
    }

    public static Map<?, ?> jsonToMap(String json) {
        if (TextUtils.isEmpty(json)) {
            return null;
        } else {
            try {
                return (Map) JacksonJsonMapper.getInstance().readValue(json, HashMap.class);
            } catch (JsonParseException var2) {
                var2.printStackTrace();
            } catch (JsonMappingException var3) {
                var3.printStackTrace();
            } catch (IOException var4) {
                var4.printStackTrace();
            }

            return null;
        }
    }

    public static <T> List<T> json2List(String json, Class<T> mclass) throws Exception {
        if (TextUtils.isEmpty(json)) {
            return null;
        } else {
            JavaType javaType = getCollectionType(ArrayList.class, new Class[]{mclass});
            List lst = null;
            lst = (List) JacksonJsonMapper.getInstance().readValue(json, javaType);
            return lst;
        }
    }

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>[] elementClasses) {
        return JacksonJsonMapper.getInstance().getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }


}


